package com.edu.his.service.impl;

import com.edu.his.domain.Disease;
import com.edu.his.domain.MedicalRecord;
import com.edu.his.domain.MedicalRecordDisease;
import com.edu.his.domain.Register;
import com.edu.his.mapper.MedicalRecordDiseaseMapper;
import com.edu.his.mapper.MedicalRecordMapper;
import com.edu.his.mapper.RegisterMapper;
import com.edu.his.service.MedicalRecordService;
import com.edu.his.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 * 创建人：飘
 * 创建时间：2024/12/16
 **/
@Service
public class MedicalRecordServiceImpl implements MedicalRecordService{

    @Autowired
    private MedicalRecordMapper medicalRecordMapper;
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private MedicalRecordDiseaseMapper medicalRecordDiseaseMapper;

    @Override
    public R insert(MedicalRecord medicalRecord,  List<Disease> list) {
        this.medicalRecordMapper.insert(medicalRecord);

        List<MedicalRecordDisease> list2=new ArrayList<>();
        for(Disease disease:list)
        {
            //病历与疾病关联的对象
            MedicalRecordDisease medicalRecordDisease=new MedicalRecordDisease();
            //疾病id
            medicalRecordDisease.setDiseaseId(disease.getId());
            //病历
            medicalRecordDisease.setMedicalRecordId(medicalRecord.getId());
            list2.add(medicalRecordDisease);
        }

        this.medicalRecordDiseaseMapper.insert(list2);

        return R.success(200,"添加成功",1);
    }

    @Transactional
    @Override
    public R update(MedicalRecord medicalRecord) {
        //1>修改病历
        this.medicalRecordMapper.update(medicalRecord);
        //2>修改挂号状态 3（本次看诊状态（1-已挂号2-医生接诊3-看诊结束4-已退号）
        //放到发药后修改状态
//        Register register=new Register();
//        register.setVisitState(3);
//        register.setId(medicalRecord.getRegisterId());
//        this.registerMapper.updateState(register);
        return R.success(200,"修改病历成功",1);
    }
}
